<?php

include_once 'data_layer.php';

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache");
header("Expires: 0");

$pids = file('participants', FILE_IGNORE_NEW_LINES);
$out = fopen("php://output", 'w');

//header
$header = array("pid", "background", "extraversion", "agreeableness", "conscientiousness", "neuroticism", "openness", "imi", "t1", "t2", "t3", "t4", "b1", "b2", "b3", "b4", "a1", "a2", "a3", "a4", "ta", "ba", "aa", "recipe");
fputcsv($out, $header);

foreach ($pids as $pid) {
    //create a row for each participant

    if (substr($pid, 0, 1) != "\\") {
        $data_row = array();
        $pt_data = getPT($pid);

        if (isset($pt_data["questions"])) {
            $pt_questions= $pt_data["questions"];
            var_dump($pt_questions);
            $pt = calculatePT($pt_questions);
            
            
            array_push($data_row, $pt["extraversion"]);
            array_push($data_row, $pt["agreeableness"]);
            array_push($data_row, $pt["conscientiousness"]);
            array_push($data_row, $pt["neuroticism"]);
            array_push($data_row, $pt["openness"]);
        }

        $motivation_data = getMotivation($pid);

        array_push($data_row, $pid);

        array_push($data_row, getBackground($pid));



        array_push($data_row, getIMI($pid) - 1);

        array_push($data_row, $motivation_data["inputQ1"] - 1);
        array_push($data_row, $motivation_data["inputQ2"] - 1);
        array_push($data_row, $motivation_data["inputQ3"] - 1);
        array_push($data_row, $motivation_data["inputQ4"] - 1);

        array_push($data_row, $motivation_data["inputQ5"] - 1);
        array_push($data_row, $motivation_data["inputQ6"] - 1);
        array_push($data_row, $motivation_data["inputQ7"] - 1);
        array_push($data_row, $motivation_data["inputQ8"] - 1);

        array_push($data_row, $motivation_data["inputQ9"] - 1);
        array_push($data_row, $motivation_data["inputQ10"] - 1);
        array_push($data_row, $motivation_data["inputQ11"] - 1);
        array_push($data_row, $motivation_data["inputQ12"] - 1);

        array_push($data_row, floatval(number_format(($motivation_data["inputQ1"] + $motivation_data["inputQ2"] + $motivation_data["inputQ3"] + $motivation_data["inputQ4"] - 4) / 4, 2, '.', '')));
        array_push($data_row, floatval(number_format(($motivation_data["inputQ5"] + $motivation_data["inputQ6"] + $motivation_data["inputQ7"] + $motivation_data["inputQ8"] - 4) / 4, 2, '.', '')));
        array_push($data_row, floatval(number_format(($motivation_data["inputQ9"] + $motivation_data["inputQ10"] + $motivation_data["inputQ11"] + $motivation_data["inputQ12"] - 4) / 4, 2, '.', '')));

        array_push($data_row, getRecipeURL($pid));
        //append the row to the csv file
        fputcsv($out, $data_row);
    }
}

fputcsv($out, array());
fputcsv($out, array());

$questions = array(
    "I enjoy using new technologies.",
    "I am interested in innovative products.",
    "I want to try the latest app releases.",
    "I like learning about high tech",
    "I enjoy tinkering with things that might be useful.",
    "I am interested in how things work.",
    "I want to fix and build things.",
    "I like to organise and put my online life in order.",
    "I enjoy experimenting with creative ideas.",
    "I am interested in sources of inspirations.",
    "I want to curate or collect sounds and images",
    "I like using attractive and aesthetic apps."
);
//fputcsv($out, array('t1', 't2', 't3', 't4', 'b1', 'b2', 'b3', 'b4', 'a1', 'a2', 'a3', 'a4'));
//fputcsv($out, $questions);
//
//close the output file
fclose($out);


